import { ElMessage, ElMessageBox } from "element-plus";

// 显示提示消息
export function messageTip(msg, type) {
    ElMessage({
        showClose: true,
        center: true,
        duration: 3000,
        message: msg,
        type: type,
    });
}

// 获取存储的token名称
export function getTokenName() {
    return "dlyk_token";
}

// 移除token
export function removeToken() {
    window.sessionStorage.removeItem(getTokenName());
    window.localStorage.removeItem(getTokenName());
}

// 弹出确认框
export function messageConfirm(msg) {
    return ElMessageBox.confirm(
        msg,
        '系统提醒',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    );
}

// 返回上一页
export function goBack() {
    this.$router.go(-1);
}

// 获取token，如果token为空则提示重新登录
export function getToken() {
    let token = window.sessionStorage.getItem(getTokenName()) || window.localStorage.getItem(getTokenName());
    if (token) {
        return token;
    } else {
        messageConfirm("请求token为空，是否重新去登录？").then(() => {
            removeToken();
            window.location.href = "/";
        }).catch(() => {
            messageTip("取消去登录", "warning");
        });
    }
}
